<?php
/**
 * Created by PhpStorm.
 * User: Hsuin
 * Date: 2017/5/13
 * Time: 10:55
 */

namespace api\controllers;


use common\controllers\ApiCommonCtrl;
use common\services\TimeReminderSve;
use Yii;

class TimeReminderController extends ApiCommonCtrl
{
    /**
     * 获取列表
     * @return \yii\web\Response
     */
    public function actionList()
    {
        //检查登录
        $this->checkLogin();
        $TimeReminderSve = new TimeReminderSve();
        $rs = $TimeReminderSve->getList(['member_id' => $this->TokenObj->member_id], 'id DESC', true);
        //处理输出数据 BY:lihouse
        if ($rs['code'] === 0) {
            foreach ($rs['data']['list'] as &$item) {
                $item['reminder'] = date('Y年m月d日', $item['reminder']);
            }
        }
        return $this->outList($rs);
    }

    //添加
    public function actionAdd()
    {
        $request = Yii::$app->request;
        if ($request->isPost) {
            //检查登录
            $this->checkLogin();
            $TimeReminderSve = new TimeReminderSve();
            $data = $request->post();
            $data['reminder'] = strtotime($data['reminder']);//UPDATE BY:lihouse
            $data['member_id'] = $this->TokenObj->member_id;
            $rs = $TimeReminderSve->add($data);
            return $rs;
        }
    }

    /*
     * 编辑
     */
    public function actionEdit()
    {
        $request = Yii::$app->request;
        //UPDATE BY:lihouse
        //检查登录
        $this->checkLogin();
        $Member = $this->TokenObj;
        $TimeReminderSve = new TimeReminderSve();
        if ($request->isPost) {
            $data = $request->post();
            if (isset($data['reminder'])) {
                $data['reminder'] = strtotime($data['reminder']);
            }
            $data['member_id'] = $Member->member_id;
            $rs = $TimeReminderSve->update($data);
            return $rs;
        }
    }


    /**
     * 删除特别时间 BY:lihouse
     * @return array
     */
    public function actionDel()
    {
        $request = Yii::$app->request;
        $TimeReminderSve = new TimeReminderSve();
        if ($request->post()) {
            self::checkLogin();
            $Member = $this->TokenObj;
            $id = $request->post('id');
            $rs = $TimeReminderSve->remove(['member_id' => $Member->member_id, 'id' => $id]);
            return $rs;
        }
    }
}